package com.game.raceroom.player	
{
	import com.util.HashMap;
	import com.game.raceroom.Room;
	public class CrashChecker
	{
		
        private var players:HashMap;
        private var room:Room;
		public  function CrashChecker(room:Room)
		{
			this.players=room.players;
			this.room=room;
		}
 		public function checkConflict():void{
 			//conflictTable=new Array;
        	for(var i:int=0;i<players.size();i++){
        		var player1:Player=(Player)(players.elementAtt(i));

				if(player1.dead)continue;

				for(var j:int=i+1;j<players.size();j++){

					var player2:Player = (Player)(players.elementAtt(j));
					if(player2.dead)continue;
					if(player1.hasAction||player2.hasAction)
						checkOneConflict(player1,player2);
				}
        	}

		}
		private var abs_x,abs_y,abs_total_0,abs_total_1,distance,distance_next,totalRadius:Number;
	
		public function checkOneConflict(actorContrl:Player,actor:Player):void{
        	 totalRadius=actorContrl.carInfo.confRadius+actor.carInfo.confRadius;
			
            //totalRadius=60;
            abs_x=actorContrl._x-actor._x;
        	abs_y=actorContrl._y-actor._y;
        	if(Math.abs(abs_x)>totalRadius ||Math.abs(abs_y)>totalRadius )return;
        		
        	abs_x=abs_x*abs_x;
        	abs_y=abs_y*abs_y;
            distance=Math.sqrt(abs_x+abs_y);
            
        	//发生碰撞
        	if(totalRadius>distance){
        		//判断是接近或离开碰撞
        		abs_x=actorContrl._x+actorContrl._vx+actorContrl.sideClipContrl.tx-(actor._x+actor._vx+actor.sideClipContrl.tx);
        		abs_y=actorContrl._y+actorContrl._vy+actorContrl.sideClipContrl.ty-(actor._y+actor._vy+actor.sideClipContrl.ty);
        	    abs_x=abs_x*abs_x;
        	    abs_y=abs_y*abs_y;
                distance_next=Math.sqrt(abs_x+abs_y);
        		
                if(distance_next<distance ){
                	makeConflictEffect(actorContrl,actor);
                }

        	}
        }
		private var crashTime:Number;
		private var absa1,absa2, double1,dx,dy,sin,cos,crash_angle,ang1,ang2,_ang1,_ang2,r1,r2,a1,a2,dx1,dy1,sideClip_dx1,sideClip_dy1,sideClip_dx2,sideClip_dy2:Number;
		private function makeConflictEffect(car1:Player,car2:Player):void {
			dx = car1._x-car2._x;
			dy = car1._y-car2._y;
			//碰撞角 
			crash_angle = Math.atan2(dy, dx); 
			//运动角 
			ang1 = Math.atan2(car1._vy, car1._vx); 
			ang2 = Math.atan2(car2._vy, car2._vx); 
			//System.out.println(car1.sideClipContrl.tx+"  ffffffffffffffffffffffffffffff   sss   fffffffffffffffffff");
			//反射角 
			_ang1 = 2*crash_angle-ang1-Math.PI; 
			_ang2 = 2*crash_angle-ang2-Math.PI; 
			//System.out.println((int)(ang1/6.28*360)+" "+(int)(ang2/6.28*360)+" "+(int)(_ang1/6.28*360)+" "+(int)(_ang2/6.28*360)+"  "+car1._angle+"  "+car2._angle);
			//运动矢量 
			r1=Math.sqrt(car1._vx*car1._vx+car1._vy*car1._vy);
			r2=Math.sqrt(car2._vx*car2._vx+car2._vy*car2._vy);
			
			//碰撞矢量 
			a1 = r1*Math.cos(crash_angle-ang1); 
			a2 = r2*Math.cos(crash_angle-ang2);  

			//碰撞矢量合成 
			dx1 = a1*Math.cos(crash_angle)+a2*Math.cos(crash_angle); 
			dy1 = a1*Math.sin(crash_angle)+a2*Math.sin(crash_angle); 
	
			//碰撞后的增量 
			sideClip_dx1 = r1*Math.cos(_ang1)+dx1; 
			sideClip_dy1 = r1*Math.sin(_ang1)+dy1; 
			sideClip_dx2 = r2*Math.cos(_ang2)+dx1; 
			sideClip_dy2 = r2*Math.sin(_ang2)+dy1; 

			double1=car1.weight/car2.weight;
			if(double1>2)double1=2;
			if(double1<0.5)double1=0.5;
			car2._dvx=sideClip_dx2*double1;
			car2._dvy=sideClip_dy2*double1;
			car2.sideClipContrl.addCrash(car2._dvx, car2._dvy);
			
			double1=car2.weight/car1.weight;
			if(double1>2)double1=2;
			if(double1<0.5)double1=0.5;
			car1._dvx=sideClip_dx1*double1;
			car1._dvy=sideClip_dy1*double1;
			car1.sideClipContrl.addCrash(car1._dvx, car1._dvy);
			
		}

	}
}